package com.ruoyi.exam.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;

import com.ruoyi.exam.domain.vo.QuestionsJoinBankVO;
import com.ruoyi.exam.domain.req.TestQuestionReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.exam.domain.TestQuestions;
import com.ruoyi.exam.service.ITestQuestionsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 试题管理Controller
 * 
 * @author ruoyi
 * @date 2024-07-01
 */
@RestController
@RequestMapping("/questions")
public class TestQuestionsController extends BaseController
{
    @Autowired
    private ITestQuestionsService testQuestionsService;

    /**
     * 查询试题管理列表
     */
    @RequiresPermissions("exam:questions:list")
    @GetMapping("/list")
    public TableDataInfo list(TestQuestions testQuestions)
    {
        startPage();
        List<TestQuestions> list = testQuestionsService.selectTestQuestionsList(testQuestions);
        return getDataTable(list);
    }

    /**
     * 导出试题管理列表
     */
    @RequiresPermissions("exam:questions:export")
    @Log(title = "试题管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TestQuestions testQuestions)
    {
        List<TestQuestions> list = testQuestionsService.selectTestQuestionsList(testQuestions);
        ExcelUtil<TestQuestions> util = new ExcelUtil<TestQuestions>(TestQuestions.class);
        util.exportExcel(response, list, "试题管理数据");
    }

    /**
     * 获取试题管理详细信息
     */
    @RequiresPermissions("exam:questions:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(testQuestionsService.selectTestQuestionsById(id));
    }

    /**
     * 新增试题管理
     */
    @RequiresPermissions("exam:questions:add")
    @Log(title = "试题管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TestQuestionReq testQuestionReq)
    {
        return toAjax(testQuestionsService.saveTestQuestions(testQuestionReq));
    }

    /**
     * 修改试题管理
     */
    @RequiresPermissions("exam:questions:edit")
    @Log(title = "试题管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TestQuestionReq testQuestionReq)
    {
        return toAjax(testQuestionsService.update(testQuestionReq));
    }

    /**
     * 删除试题管理
     */
    @RequiresPermissions("exam:questions:remove")
    @Log(title = "试题管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids)
    {
        return toAjax(testQuestionsService.deleteTestQuestionsByIds(ids));
    }

    /**
     * 试题加入题库
     */
   // @RequiresPermissions("exam:questions:questionsJoinBank")
   // @Log(title = "试题加入题库", businessType = BusinessType.INSERT)
    @PostMapping("/questionsJoinBank")
    public AjaxResult questionsJoinBank(@RequestBody QuestionsJoinBankVO questionsJoinBankVO)
    {
        return toAjax(testQuestionsService.questionsJoinBank(questionsJoinBankVO.getTestQuestionsIds(),questionsJoinBankVO.getQuestionBankIds()));
    }


    /**
     * 试题移除
     */
    // @RequiresPermissions("exam:questions:questionsJoinBank")
    // @Log(title = "试题加入题库", businessType = BusinessType.INSERT)
    @DeleteMapping("/questionsJoinBank")
    public AjaxResult questionsDelBank(@RequestBody QuestionsJoinBankVO questionsJoinBankVO)
    {
        return toAjax(testQuestionsService.questionsDelBank(questionsJoinBankVO.getTestQuestionsIds(),questionsJoinBankVO.getQuestionBankIds()));
    }



    @PutMapping("/addQuestionsToBank")
    @Validated
    public AjaxResult addQuestionsToBank(@NotEmpty @RequestParam("testQuestionsIds") List<Long> testQuestionsIds,@NotEmpty @RequestParam("questionBankIds") List<Long> questionBankIds)
    {
        return toAjax(testQuestionsService.addQuestionsToBank(testQuestionsIds,questionBankIds));
    }


    @DeleteMapping("/delQuestionsToBank")
    @Validated
    public AjaxResult delQuestionsToBank(@NotEmpty @RequestParam("testQuestionsIds") List<Long> testQuestionsIds,@NotEmpty @RequestParam("questionBankIds") List<Long> questionBankIds)
    {
        return toAjax(testQuestionsService.delQuestionsToBank(testQuestionsIds,questionBankIds));
    }





}
